package cn.ddy.hellomvc.controller;

import cn.ddy.hellomvc.beans.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author ddy
 * @create 2020-08-18 14:37:27
 */
@Controller
@RequestMapping("hello")
@SessionAttributes("loginUser")  //将loginUser作用范围升为session，即request和session都能用
public class HelloController {

    @RequestMapping("hello")
    // /WEB-INF/ok.jsp
    //@ResponseBody
    public String hello(Map<String,String> map) {
        map.put("msg","hellomvc");
        return "ok"; //逻辑视图名，跟前缀、后缀拼接之后，构成物理视图
    }

    @GetMapping("del/{id}")
    public String del(@PathVariable(name = "id",required = false) Integer id){
        System.out.println(id);
        return "ok";
    }

    @GetMapping("getParam")
    public String del(@RequestParam("id") String id,
                      @RequestParam(name = "pageNum",required = false,defaultValue = "1") Integer pageNum) {
        System.out.println("=====>"+id+":"+pageNum);
        return "ok";
    }

    @PostMapping("add")
    public String add(User user) {
        System.out.println("====>" +user);
        return "ok";
    }

    @GetMapping("model")
    public ModelAndView model(Map<String,Object> map, Model model, ModelMap modelMap) {
        final ModelAndView mv = new ModelAndView();
        mv.addObject("mvkey","mvValue");
        map.put("mapKey","mapVal");
        model.addAttribute("modelKey","modelVal");
        modelMap.addAttribute("modelMapKey","modelMapVal");

        //获取每个的类型
        System.out.println(map.getClass());
        System.out.println(model.getClass());
        System.out.println(modelMap.getClass());
        System.out.println(map == model);
        System.out.println(map == modelMap);
        //需要设置逻辑视图名
        mv.setViewName("ok");
        return mv;
    }

    //跳转到修改页面
    @GetMapping("toUpdate")
    public String toUpdate(/*Map<String,User> map*/) {
        System.out.println("toupdate enter.....");
        //应该根据id去数据库查询
        /*User user = new User();
        user.setUserName("张三");
        user.setId(1001);
        user.setSal(10011.00f);
        user.setHobby(new String[]{"lq","ls"});
        map.put("user",user);*/
        return "update";
    }

    @PostMapping("update")
    public String update(User user) {
        System.out.println("====>update"+user);
        //应该作为参数，向service传递
        //userService.update(user)
        return "ok";
    }

    @ModelAttribute
    public void getUserById(@RequestParam(value="id",required=false) Integer id,
                            Map<String,Object> map) {
        System.out.println("come in @ModelAttribute......");
        if (null != id) {
            //模拟根据ID从数据库查询出user对象的原始数据。
            User user = new User();
            user.setUserName("张三");
            user.setId(1001);
            user.setSal(10011.00f);
            user.setHobby(new String[]{"lq","ls"});
            user.setEmail("aaaaa");
            map.put("user", user);
        }
    }

    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Map<String,Object> map)
    {
        User user = new User();
        user.setUserName("张三");
        user.setId(1001);
        user.setSal(10011.00f);
        user.setHobby(new String[]{"lq","ls"});
        user.setEmail("aaaaa");
        map.put("loginUser",user);
        map.put("nickName","狗蛋");
        return "ok";
    }

}
